package org.jeecg.sms.util;

import java.security.MessageDigest;

/**
 * The class used to generate check code of cooperator.
 *
 * @author <a href="mailto:liujie.han@hp.com">JetHan</a>
 */
public class SlpMD5 {

    public static String generateCheckCode(String source) {
        String result = null;
        // Used to convert 16-byte hexadecimal characters.
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(source.getBytes("UTF-8"));
            // MD5 calculation is a 128-bit long integer, that is with 16-byte byte.
            byte tmp[] = md.digest();
            char str[] = new char[16 * 2];
            int k = 0;
            // Convert each byte to hexadecimal of MD5.
            for (int i = 0; i < 16; i++) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            // Convert the result from byte to string.
            result = new String(str);
        } catch (Exception e) {
        }
        return result;
    }


}
